#include "StdAfx.h"
#include "DlgMain.h"

CDlgMain::CDlgMain(void *pController):m_pController(pController)
{
}

CDlgMain::~CDlgMain(void)
{
}

LRESULT CDlgMain::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// center the dialog on the screen
	CenterWindow();

	// set icons
	//	HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
	//	SetIcon(hIcon, TRUE);
	//	HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
	//	SetIcon(hIconSmall, FALSE);

	// load friends from local. add them to the list.

	if (m_pController)
	{
		CMyController* pCtl = (CMyController*)m_pController;
		CUser *pUser = pCtl->GetUsers();
	}
	CTreeViewCtrl tv;
	tv.Attach(GetDlgItem(IDC_TREE1));
	HTREEITEM hti, hti2;
	hti = tv.InsertItem(_T("foo"), TVI_ROOT, TVI_LAST );
	hti = tv.InsertItem(_T("foo"), TVI_ROOT, TVI_LAST );
	hti = tv.InsertItem(_T("foo"), TVI_ROOT, TVI_LAST );
	hti = tv.InsertItem(_T("foo"), TVI_ROOT, TVI_LAST );
	hti2 = tv.InsertItem(_T("bar"), hti, TVI_LAST );
	hti2 = tv.InsertItem(_T("bar"), hti, TVI_LAST );
	hti2 = tv.InsertItem(_T("bar"), hti, TVI_LAST );
	hti2 = tv.InsertItem(_T("bar"), hti, TVI_LAST );
	tv.Detach();

	return TRUE;
}

LRESULT CDlgMain::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// TODO: Add validation code 

//	EndDialog(wID);
	Logout();

	return 0;
}

LRESULT CDlgMain::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (m_pController)
	{
		CMyController* pCtl = (CMyController*)m_pController;
		pCtl->TermServer(m_hWnd);
	};

	EndDialog(wID);
	return 0;
}

void CDlgMain::Logout()
{
	if (m_pController)
	{
		CMyController* pCtl = (CMyController*)m_pController;
		pCtl->Logout(m_hWnd);	
	}
}

LRESULT CDlgMain::OnResponse(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	if (wParam > 0)
		EndDialog(0);

	return 0;
}